package com.dwxt.cdms.service;

import com.dwxt.cdms.admin.service.UserServiceAdmin;
import com.dwxt.cdms.base.Page;
import com.dwxt.cdms.dao.CustomerMapper;
import com.dwxt.cdms.dao.SetmealMapper;
import com.dwxt.cdms.entity.Customer;
import com.dwxt.cdms.entity.Setmeal;
import com.dwxt.cdms.entity.SetmealExample;
import com.dwxt.cdms.entity.vo.SetmealVO;
import com.dwxt.common.base.BaseResult;
import com.dwxt.common.base.StatusCode;
import com.dwxt.common.util.JsonUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.stream.Collectors;

@Service
public class SetmealService {

    @Autowired
    private SetmealMapper setmealMapper;

    @Autowired
    private UserServiceAdmin userServiceAdmin;

    @Autowired
    private CustomerMapper customerMapper;

    public List<SetmealVO> page() {
        /*PageHelper.startPage(page.getPageNum(), page.getPageSize());
        SetmealExample example = new SetmealExample();
        example.setOrderByClause("create_time desc");
        List<Setmeal> setmealList = setmealMapper.selectByExample(example);
        PageInfo pageInfo = new PageInfo(setmealList);
        return BaseResult.ok(pageInfo);*/
        SetmealExample example = new SetmealExample();
        example.createCriteria().andParentIdEqualTo(0);
        List<Setmeal> setmeals = setmealMapper.selectByExample(example);

        List<SetmealVO> setmealVOS = JsonUtils.jsonToList(JsonUtils.objectToJson(setmeals), SetmealVO.class);
        for (SetmealVO setmealVO : setmealVOS) {
            example.clear();
            example.setOrderByClause("create_time");
            example.createCriteria().andParentIdEqualTo(setmealVO.getId());
            List<Setmeal> childSetmeals = setmealMapper.selectByExample(example);

            List<SetmealVO> setmealVOList = JsonUtils.jsonToList(JsonUtils.objectToJson(childSetmeals), SetmealVO.class);
            for (SetmealVO vo : setmealVOList) {
                example.clear();
                example.setOrderByClause("create_time");
                example.createCriteria().andParentIdEqualTo(vo.getId());
                List<Setmeal> childSetmealList = setmealMapper.selectByExample(example);
                vo.setChildren(JsonUtils.jsonToList(JsonUtils.objectToJson(childSetmealList), SetmealVO.class));
            }

            setmealVO.setChildren(setmealVOList);
        }
        return setmealVOS;
    }

    public BaseResult list() {
        SetmealExample example = new SetmealExample();
        example.setOrderByClause("create_time desc");
        List<Setmeal> setmealList = setmealMapper.selectByExample(example);
        return BaseResult.ok(setmealList);
    }

    public BaseResult add(Setmeal setmeal) {
        setmealMapper.insertSelective(setmeal);
        return BaseResult.ok();
    }

    public BaseResult delete(Integer id) {
        List<Customer> customers = customerMapper.selectCustomerList(null);
        for (Customer customer : customers) {
            if (StringUtils.isNotBlank(customer.getSetmeal())) {
                String[] split = customer.getSetmeal().split(",");
                for (String s : split) {
                    if (id.equals(Integer.valueOf(s))) {
                        return BaseResult.build(StatusCode.CAN_NOT_DELETE);
                    }
                }
            }
        }
        setmealMapper.deleteByPrimaryKey(id);
        return BaseResult.ok();
    }


    public BaseResult update(Setmeal id) {
        setmealMapper.updateByPrimaryKeySelective(id);
        return BaseResult.ok();
    }

    public String selectSetmealStr(List list) {
        if (list == null || list.size() == 0) {
            return "";
        }
        SetmealExample example = new SetmealExample();
        SetmealExample.Criteria criteria = example.createCriteria();
        criteria.andIdIn(list);
        List<Setmeal> setmeals = setmealMapper.selectByExample(example);
        List<String> collect = setmeals.stream().map(Setmeal::getName).collect(Collectors.toList());
        String join = StringUtils.join(collect, "|");
        return join;
    }

    public BaseResult selectPageByCustomerId(Page page, Integer customerId) {
        Customer customer = customerMapper.selectByPrimaryKey(customerId);
        String setmealStr = customer.getSetmeal();
        if (setmealStr == null || "".equals(setmealStr)) {
            return BaseResult.build(StatusCode.NO_SETMEAL);
        }
        String[] split = customer.getSetmeal().split(",");
        List<Integer> ids = Arrays.stream(split).map(s -> Integer.valueOf(s)).collect(Collectors.toList());
        SetmealExample example = new SetmealExample();
        SetmealExample.Criteria criteria = example.createCriteria();
        criteria.andIdIn(ids).andParentIdNotEqualTo(0);
        List<Setmeal> list = setmealMapper.selectByExample(example);
        List<Map> collect = list.stream().map(setmeal -> {
            Map map = JsonUtils.jsonToPojo(JsonUtils.objectToJson(setmeal), Map.class);
            if (customer.getBuyTime() != null) {
                Calendar cal = Calendar.getInstance();
                cal.setTime(customer.getBuyTime());
                cal.add(Calendar.YEAR, 1);
                Date time = cal.getTime();
                Date now = new Date();
                if (time.getTime() > now.getTime()) {//未到期
                    map.put("remainTime", time.getTime() - now.getTime());
                } else {//到期了
                    map.put("remainTime", "0");
                }
            }
            return map;
        }).collect(Collectors.toList());
        return BaseResult.ok(collect);
    }

}
